package com.ruoyi.utils;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.domain.Message;
import com.ruoyi.domain.TextMessage;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;


import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @description: 消息处理器
 * @author: DreamflyChen
 * @create: 2020-08-30 13:33
 */
@Slf4j
public class SendRobotMessageUtil {

    /**
     * 打分推送
     * @param phone
     * @throws Exception
     */
    public static void ShareTip(String phone) throws Exception {

        String webHookAddress = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8e32fbe9-8a3f-41a5-ae3e-dc5e0df0989c";
        //发送文本消息
        Message message = new Message();
        message.setMsgtype("text");

        TextMessage textMessage = new TextMessage();
        textMessage.setContent("我已经完成自评,需要您帮我审核一下！！！");
//        textMessage.setMentioned_list(Arrays.asList("@phil"));
        textMessage.setMentioned_mobile_list(Arrays.asList(phone));
        JSONObject text = JSONObject.parseObject(JSONObject.toJSONString(textMessage));
        message.setText(text);
        sendMessage(message, webHookAddress);



    }

    /**
     * 问题推送
     * @param name
     * @param phone
     * @throws Exception
     */
    public static void helpNotice(String name, String phone) throws Exception {
        String webHookAddress = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8e32fbe9-8a3f-41a5-ae3e-dc5e0df0989c";
        //发送文本消息
        Message message = new Message();
        message.setMsgtype("text");

        TextMessage textMessage = new TextMessage();
        textMessage.setContent("小" + name + ", 宁有一条就业帮扶请及时查看");
//        textMessage.setMentioned_list(Arrays.asList("@phil"));
        textMessage.setMentioned_mobile_list(Arrays.asList(phone, "@all"));
        JSONObject text = JSONObject.parseObject(JSONObject.toJSONString(textMessage));
        message.setText(text);
        sendMessage(message, webHookAddress);


    }

    /**
     * 日报未填写提醒
     * @throws Exception
     */
    public static void dailyReminder() throws Exception {
        String webHookAddress = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8e32fbe9-8a3f-41a5-ae3e-dc5e0df0989c";
        //发送文本消息
        Message message = new Message();
        message.setMsgtype("text");

        TextMessage textMessage = new TextMessage();
        textMessage.setContent("未提交日报的老师请尽快提交！！！");
        textMessage.setMentioned_mobile_list(Arrays.asList("@all"));
        JSONObject text = JSONObject.parseObject(JSONObject.toJSONString(textMessage));
        message.setText(text);
        sendMessage(message, webHookAddress);


    }


    private static boolean sendMessage(Message message, String webHookAddress) {
        OkHttpClient client = new OkHttpClient.Builder()
                // 设置连接超时时间
                .connectTimeout(10, TimeUnit.SECONDS)
                // 设置读取超时时间
                .readTimeout(20, TimeUnit.SECONDS)
                .build();
        MediaType contentType = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSONObject.toJSONString(message), contentType);
        Request request = new Request.Builder().url(webHookAddress).post(body).addHeader("cache-control", "no-cache").build();
        try {
            Response response = client.newCall(request).execute();
            byte[] datas = response.body().bytes();
            String respMsg = new String(datas);
            JSONObject resultJSON = JSONObject.parseObject(respMsg);
            if (resultJSON.getIntValue("errcode") == 0) {
                log.info("消息发送成功!");
                return true;
            }
            log.error("消息发送失败, 错误信息如下: {}", resultJSON.getString("errmsg"));
            return false;
        } catch (IOException e) {
            log.error("消息发送失败, 异常信息如下: {}", e.getMessage());
            return false;
        }
    }
}
